// pages/assess-page/assess-page.js
var app = getApp()
Page({

  /**
   * 生命周期函数--监听页面加载
   */

  data:{
    formId:'',
    form_title:'',
    province:'',
    city:'',
    infoId:'',
    
    },

  onLoad: function(options) {
    wx.showShareMenu({
      withShareTicket: true
    })

    var that = this;
    var infoId = options.infoId;
    that.setData({
      infoId: infoId
    })
    wx.request({
      url: app.globalData.base_url + 'species/getFormInfo',
      header: {
        'Content-Type': 'application/json;charset=UTF-8;'
      },
      method: "POST",
      data: {
        "CUSTOMER_ID": wx.getStorageSync('user_id'),
        "infoId": infoId,
      },
      success: function (res) {
        console.log(res)
        that.setData({
          formId: res.data.data.FORM_ID,
          form_title: res.data.data.FORM_TITLE,
          province: res.data.data.PROVINCE,
          city: res.data.data.CITY,
          species_name: res.data.data.SPECIES_NAME,
          xj: res.data.data.XJ,
          gd: res.data.data.GD,
          gf: res.data.data.GF,
          dj: res.data.data.DJ,
          gj: res.data.data.GJ,
          num: res.data.data.NUM,
        })

      }
    })

  },

  addSpeciesForm: function (e) {
    var that = this;
    var formId = that.data.formId;
    var infoId = that.data.infoId;
    var item = e.detail.value;
    var species_name = item.species_name;
    var xj = item.xj;
    var gd = item.gd;
    var gf = item.gf;
    var dj = item.dj;
    var gj = item.gj;
    var num = item.num;
    if (species_name.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请填写苗木名称',
        showCancel: false,
      })
      return;
    }

    if (xj.length == 0 && gd.length == 0 && gf.length == 0 && dj.length == 0 && gj.length == 0) {
      wx.showModal({
        title: '提示',
        content: '胸径，高度，冠辐，地径，杆径 请填且仅填写一项',
        showCancel: false,
      })
      return;
    }

    wx.request({
      url: app.globalData.base_url + 'species/editAssessFormInfo',
      header: {
        'Content-Type': 'application/json;charset=UTF-8;'
      },
      method: "POST",
      data: {
        "CUSTOMER_ID": wx.getStorageSync('user_id'),
        "infoId": infoId,
        "species_name": species_name,
        "xj": xj,
        "gd": gd,
        "gf": gf,
        "dj": dj,
        "gj": gj,
        "num": num
      },
      success: function (res) {
          wx.navigateTo({
            url: '/pages/assess-list/assess-list?formId='+formId,
          })   

      }
    })


 
  },  

  //分享
  onShareAppMessage: function (res) {
    let that = this
    return {
      title: '找苗木网',
      path: '/pages/login1/login1',
      imageUrl: '../../source/icon/banner.png',
      success: function (res) {
        console.log(res.shareTickets)
        //getSystemInfo是为了获取当前设备信息，判断是android还是ios，如果是android
        //还需要调用wx.getShareInfo()，只有当成功回调才是转发群，ios就只需判断shareTickets
        //获取用户设备信息
        wx.getSystemInfo({
          success: function (d) {
            console.log(d);
            if (res.shareTickets != undefined) {
              console.log("分享的是群");
              wx.getShareInfo({
                shareTicket: res.shareTickets[0],
                success: function (res) {
                  //分享到群之后你要做的事情
                  wx.request({
                    url: app.globalData.base_url + "Customer/grantTicketByGroup",
                    header: {
                      'Content-Type': 'application/json;charset=UTF-8;'
                    },
                    method: "POST",
                    data: {
                      "CUSTOMER_ID": wx.getStorageSync('user_id'),
                    },
                    complete: function (res) {
                      that.setData({

                      });
                      if (res == null || res.data == null) {
                        console.error('网络请求失败');
                        return;
                      }
                    },
                    success: function (res) {
                      if (res.data.code === "200") {
                        wx.showModal({
                          title: '提示',
                          content: '赠送您一张估价券哦，有效时间24小时',
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                            } else if (res.cancel) {
                              console.log('用户点击取消')
                            }
                          }
                        })
                      } else if (res.data.code === "203") {
                        var maxGrantCount = res.data.data;
                        wx.showModal({
                          title: '提示',
                          content: '分享到群每天最多赠送 ' + maxGrantCount + ' 张您已达到上限',
                          success: function (res) {
                            if (res.confirm) {
                              console.log('用户点击确定')
                            } else if (res.cancel) {
                              console.log('用户点击取消')
                            }
                          }
                        })
                      }
                    }
                  })
                }
              })
            } else {//分享到个人要做的事情，我给的是一个提示
              console.log("分享的是个人");
              wx.showModal({
                title: '提示',
                content: '分享到群赠送估价券哦',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            }
          },
          fail: function (res) {

          }
        })
      }

    }
  },

  
})